package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;

import com.example.demo.mapper.NewsMapper;
import com.example.demo.po.News;
import com.example.demo.po.dto.NewsParam;
import com.example.demo.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    NewsMapper newsMap;

    @Autowired
    NewsService newsService;



    @GetMapping("/get")
    @ResponseBody
    public Result GetNews(){
        List<News> NewsList = new ArrayList();
        NewsList = newsMap.GetNews();

        return ResultUtil.success(NewsList);
    }


    @GetMapping("/getmaterial")
    @ResponseBody
    public Result GetMaterialNews(){
        List<News> NewsList = new ArrayList();
        NewsList = newsMap.GetMaterialNews();

        return ResultUtil.success(NewsList);
    }


    @GetMapping("/getfrontmaterial")
    @ResponseBody
    public Result GetFrontMaterialNews(){
        List<News> NewsList = new ArrayList();
        NewsList = newsMap.GetMaterialNewsFront();


        return ResultUtil.success(NewsList);
    }



    @GetMapping("/getreport")
    @ResponseBody
    public Result GetReportNews(){
        List<News> NewsList = new ArrayList();
        NewsList = newsMap.GetReportNews();

        return ResultUtil.success(NewsList);
    }

    @GetMapping("/getfrontreport")
    @ResponseBody
    public Result GetFrontreportNews(){
        List<News> NewsList = new ArrayList();
        NewsList = newsMap.GetReportNewsFront();

        return ResultUtil.success(NewsList);
    }


    @GetMapping("/get/{id}")
    public Result GetNewsById(@PathVariable int id){

        System.out.println(id);
       News news = newsMap.GetNewsById(id);
       return ResultUtil.success(news);

    }


    @GetMapping("/search")
    @ResponseBody
    public Result SearchNews( @RequestParam("name")  String name){

        if(name == null|| name.isEmpty())
        {
            return ResultUtil.success(newsMap.GetNews()) ;
        }
        List<News> NewsList = new ArrayList();
        NewsList = newsMap.GetNewsByName("%"+name+"%");
        return ResultUtil.success(NewsList);
    }

    @PostMapping("/save")
    @ResponseBody
    public Result SaveNews(@RequestBody News _news){

        Result res = newsService.SaveNews(_news);

        return res;
    }

    @GetMapping("/delete/{id}")
    @ResponseBody
    public Result DeleteNews(@PathVariable int id){
        newsMap.DeleteNews(id);
        return ResultUtil.success("删除成功");
    }

    @PostMapping("/update")
    public Result UpdateNews(@RequestBody News newspo){

       // System.out.println(newspo.getContent());

        newsService.UpdateNews(newspo);
        return ResultUtil.success("更新成功");
    }

}
